package lpl.com.xyd.test.manage.common.utils.excel;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.NumberFormat;
import java.util.List;
import java.util.Map;

@Component
public class ExcelMain {

    public boolean excute() {
        try {
            Map<String, List<JSONObject>> map = ExcelUtils.readfile("C:\\Users\\andy\\Desktop\\Excel");
            int[] caseNo = {0};
            int[] defectNo = {0};
            List<JSONObject> objects = map.get("key2");
            objects.forEach(v ->{
                caseNo[0] = caseNo[0] + Integer.valueOf(null != v.get("caseNo")?v.get("caseNo")+"":"0");
                defectNo[0] = defectNo[0] + Integer.valueOf(null != v.get("defectNo")?v.get("defectNo")+"":"0");
            });
            // 创建一个数值格式化对象
            NumberFormat numberFormat = NumberFormat.getInstance();
            // 设置精确到小数点后2位
            numberFormat.setMaximumFractionDigits(2);
            String defectRate = numberFormat.format((double) defectNo[0]/(double)caseNo[0]*100) +"%";
            JSONObject object = new JSONObject();

            object.put("no","");
            object.put("name","");
            object.put("status","");
            object.put("pm","");
            object.put("notice","");
            object.put("dev","");
            object.put("qa","");
            object.put("group","");
            object.put("remarks","");

            object.put("caseNo",String.valueOf(caseNo[0]));
            object.put("defectNo",String.valueOf(defectNo[0]));
            object.put("defectRate",defectRate);
            objects.add(object);
            ExcelUtils.testWrit("D:\\code",map);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
